#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Person 
{
    char *name;
    int age;
};

struct Person** allocateSpeace()
{
    struct Person **temp = malloc(sizeof(struct Person *) * 3);
    for (int i = 0; i < 3; i++)
    {
        temp[i] = malloc(sizeof(struct Person));
        temp[i]->name = malloc(sizeof(char)*64);
        sprintf(temp[i]->name, "Name_%d", i + 1);
        temp[i]->age = i+20;
    }

    return temp;
}

void printPersons(struct Person **persons)
{
    if(persons == NULL)
    {
        return;
    }

    for (int i = 0; i < 3; ++i)
    {
        printf("Name:%s Age:%d\n", persons[i]->name, persons[i]->age);
    }
}

void freeSpace(struct Person **persons)
{
    if(persons == NULL)
    {
        return;
    }
    
    for (int i = 0; i < 3; ++i)
    {
        if(persons[i] == NULL){
            continue;
        }

        if(persons[i]->name != NULL)
        {
            printf("%s的内存被释放!\n", persons[i]->name);
            free(persons[i]->name);
            persons[i]->name = NULL;
        }

        free(persons[i]);
        persons[i] = NULL;
    }

    free(persons);
    persons = NULL;
}

void test01()
{
    struct Person **persons = NULL;   
    persons = allocateSpeace();
    printPersons(persons);
    freeSpace(persons);
}

int main(int argc, char const *argv[])
{
    test01();
    return 0;
}
